package com.uzai.console.service.sop.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.uzai.common.enums.MerConfigKeyEnum;
import com.uzai.common.enums.MerConfigTypeEnum;
import com.uzai.common.enums.RedisCacheKeyEnum;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.dto.sop.sopriskalarm.SopRiskAlarmDto;
import com.uzai.console.dto.sop.sopriskalarm.SopRiskAlarmOrderDto;
import com.uzai.console.entity.MerBaseConfig;
import com.uzai.console.mapper.MerBaseConfigMapper;
import com.uzai.console.service.sop.SopRiskAlarmService;
import com.uzai.console.vo.sop.sopriskalarm.SopRiskAlarmOrderVo;
import com.uzai.console.vo.sop.sopriskalarm.SopRiskAlarmVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author liuqi
 * @date 2021年08月23日 20:25
 */
@Service
@Slf4j
public class SopRiskAlarmServiceImpl implements SopRiskAlarmService {

    @Autowired
    private MerBaseConfigMapper merBaseConfigMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询提现风控详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return null
     */
    public SopRiskAlarmVo findSopRiskAlarm(Long merId) {

        SopRiskAlarmVo sopRiskAlarmVo = new SopRiskAlarmVo();

        //查询运营商提现风控信息
        MerBaseConfig merBaseConfig = merBaseConfigMapper.selectByMerId(merId, MerConfigKeyEnum.SOP_RISK_ALARM.getValue());
        if (merBaseConfig != null) {
            try {
                sopRiskAlarmVo = JSONObject.parseObject(merBaseConfig.getVal(), SopRiskAlarmVo.class);
            }catch (Exception e){
                log.error("SOP的提现风控配置数据格式有误，error={}", e.getMessage());
            }

        }
        return sopRiskAlarmVo;
    }

    /**
     * 修改提现风控详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param sopRiskAlarmDto
     * @return null
     */
    public void updateSopRiskAlarm(SopRiskAlarmDto sopRiskAlarmDto){

        SopRiskAlarmVo sopRiskAlarmVo = new SopRiskAlarmVo();
        BeanUtils.copyProperties(sopRiskAlarmDto, sopRiskAlarmVo);

        MerBaseConfig merBaseConfig = merBaseConfigMapper.selectByMerId(sopRiskAlarmDto.getMerId(), MerConfigKeyEnum.SOP_RISK_ALARM.getValue());

        Integer now = DateUtil.getNowTime();
        //修改
        if (merBaseConfig != null){
            merBaseConfig.setVal(Tools.getStrEmpty(JSONObject.toJSONString(sopRiskAlarmVo)));
            merBaseConfig.setUpdateTime(now);
            merBaseConfigMapper.update(merBaseConfig);
        } else { //增加
            merBaseConfig = new MerBaseConfig();
            merBaseConfig.setId(IdWorker.getId());
            merBaseConfig.setMerId(sopRiskAlarmDto.getMerId());
            merBaseConfig.setType(MerConfigTypeEnum.PRODUCTS.getValue());
            merBaseConfig.setKey(MerConfigKeyEnum.SOP_RISK_ALARM.getValue());
            merBaseConfig.setVal(Tools.getStrEmpty(JSONObject.toJSONString(sopRiskAlarmVo)));
            merBaseConfig.setCreateTime(now);
            merBaseConfig.setUpdateTime(now);
            merBaseConfigMapper.insert(merBaseConfig);
        }

        //保存到Redis当中
        redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.SOP_RISK_ALARM.getKey(), sopRiskAlarmDto.getMerId()), merBaseConfig.getVal());
    }

    /**
     * 查询订单风控详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return null
     */
    public SopRiskAlarmOrderVo findSopRiskAlarmOrder(Long merId) {

        SopRiskAlarmOrderVo sopRiskAlarmOrderVo = new SopRiskAlarmOrderVo();

        //查询运营商提现风控信息
        MerBaseConfig merBaseConfig = merBaseConfigMapper.selectByMerId(merId, MerConfigKeyEnum.SOP_RISK_ALARM_ORDER.getValue());
        if (merBaseConfig != null) {
            try {
                sopRiskAlarmOrderVo = JSONObject.parseObject(merBaseConfig.getVal(), SopRiskAlarmOrderVo.class);
            }catch (Exception e){
                log.error("SOP的订单风控配置数据格式有误，error={}", e.getMessage());
            }

        }
        return sopRiskAlarmOrderVo;
    }

    /**
     * 修改订单风控详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param sopRiskAlarmOrderDto
     * @return null
     */
    public void updateSopRiskAlarmOrder(SopRiskAlarmOrderDto sopRiskAlarmOrderDto){

        SopRiskAlarmOrderVo sopRiskAlarmOrderVo = new SopRiskAlarmOrderVo();
        BeanUtils.copyProperties(sopRiskAlarmOrderDto, sopRiskAlarmOrderVo);

        MerBaseConfig merBaseConfig = merBaseConfigMapper.selectByMerId(sopRiskAlarmOrderDto.getMerId(), MerConfigKeyEnum.SOP_RISK_ALARM_ORDER.getValue());

        Integer now = DateUtil.getNowTime();
        //修改
        if (merBaseConfig != null){
            merBaseConfig.setVal(Tools.getStrEmpty(JSONObject.toJSONString(sopRiskAlarmOrderVo)));
            merBaseConfig.setUpdateTime(now);
            merBaseConfigMapper.update(merBaseConfig);
        } else { //增加
            merBaseConfig = new MerBaseConfig();
            merBaseConfig.setId(IdWorker.getId());
            merBaseConfig.setMerId(sopRiskAlarmOrderDto.getMerId());
            merBaseConfig.setType(MerConfigTypeEnum.PRODUCTS.getValue());
            merBaseConfig.setKey(MerConfigKeyEnum.SOP_RISK_ALARM_ORDER.getValue());
            merBaseConfig.setVal(Tools.getStrEmpty(JSONObject.toJSONString(sopRiskAlarmOrderVo)));
            merBaseConfig.setCreateTime(now);
            merBaseConfig.setUpdateTime(now);
            merBaseConfigMapper.insert(merBaseConfig);
        }

        //保存到Redis当中
        redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.SOP_RISK_ALARM_ORDER.getKey(), sopRiskAlarmOrderDto.getMerId()), merBaseConfig.getVal());
    }

}
